package UILayer;

import CtrLayer.StaffCtr;
import ModelLayer.CurrentAccount;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.border.*;

import java.awt.Dimension;

public class AccountsUI extends JInternalFrame
{

	private StaffCtr staff_ctr;
	private JTextField search_tf;
	private JPanel panel_1;
	private JTable staff_table;
	private String[] column_names = { "ID", "Name", "Account type", "Amount sold" };

	/**
	 * Create the frame.
	 */
	public AccountsUI()
	{
		super("Accounts", true, true, true, true);
		setBounds(300, 100, 513, 348);
		staff_ctr = new StaffCtr();
		panel_1 = new JPanel();
		getContentPane().add(panel_1, BorderLayout.NORTH);
		panel_1.setLayout(new BorderLayout(0, 0));
		this.addInternalFrameListener(new InternalFrameAdapter()
		{
			public void internalFrameActivated(InternalFrameEvent e)
			{
				refresh();
			}
		});
		make_search();
		make_ctr_btns();
		make_table();
	}
	
	private void refresh()
	{
		Object[][] filling = staff_ctr.give_all_accounts(CurrentAccount.give_rank().equals("owner"));
		

		staff_table.setModel(new MyTableModel(filling, column_names));
		staff_table.getColumnModel().getColumn(0).setPreferredWidth(80);
		staff_table.getColumnModel().getColumn(1).setPreferredWidth(120);
	}
	
	private void search()
	{
		Object[][] filling;
		String contents = search_tf.getText();
		if (is_numeric(contents))
		{
			filling = new Object[1][4];
			filling[0] = staff_ctr.give_single_account(contents, CurrentAccount.give_rank().equals("owner"));
			
		}
		else
		{
			filling = staff_ctr.give_accounts_via_name(contents, CurrentAccount.give_rank().equals("owner"));
		}
		staff_table.setModel(new MyTableModel(filling, column_names));
		staff_table.getColumnModel().getColumn(0).setPreferredWidth(80);
		staff_table.getColumnModel().getColumn(1).setPreferredWidth(120);
	}
	
	private void make_search()
	{
		
		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2, BorderLayout.WEST);
		panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JLabel search_lbl = new JLabel("Sale number");
		panel_2.add(search_lbl);

		search_tf = new JTextField();
		panel_2.add(search_tf);
		search_tf.setColumns(10);

		JButton search_btn = new JButton("Search");
		search_btn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				search();
			}
		});
		panel_2.add(search_btn);
		
		JButton clear_btn = new JButton("Clear");
		clear_btn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				refresh();
				search_tf.setText("");
			}
		});
		panel_2.add(clear_btn);
	}
	
	private void make_table()
	{

		final Object[][] filling = staff_ctr.give_all_accounts(CurrentAccount.give_rank().equals("owner"));

		staff_table = new JTable();

		staff_table.setFillsViewportHeight(true);
		staff_table.setModel(new MyTableModel(filling, column_names));

		staff_table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		staff_table.setAutoCreateRowSorter(true);
		staff_table.setRowSelectionAllowed(true);
		staff_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		staff_table.getColumnModel().getColumn(0).setPreferredWidth(80);
		staff_table.getColumnModel().getColumn(1).setPreferredWidth(120);
		staff_table.getTableHeader().setReorderingAllowed(false);
		staff_table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

		staff_table.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent e)
			{
				if (e.getClickCount() == 2)
				{
					details();
				}
			}
		});
		
		DefaultTableCellRenderer right_renderer = new DefaultTableCellRenderer();
		right_renderer.setHorizontalAlignment(JLabel.RIGHT);
		staff_table.setDefaultRenderer(String.class, right_renderer);

		JScrollPane scroll_pane = new JScrollPane(staff_table);
		scroll_pane.setBorder(new EmptyBorder(5, 10, 5, 10));
		getContentPane().add(scroll_pane, BorderLayout.CENTER);
	}
	
	private void make_ctr_btns()
	{
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.SOUTH);
		panel.setLayout(new BorderLayout(0, 0));

		JButton details_btn = new JButton("Details");
		details_btn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				details();
			}
		});
		
		
		JPanel panel_2 = new JPanel();
		panel.add(panel_2, BorderLayout.EAST);
		panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JButton btnNew = new JButton("New");
		btnNew.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				add();
			}
		});
		panel_2.add(btnNew);
		
		JButton btnEdit = new JButton("Edit");
		btnEdit.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				edit();
			}
		});
		panel_2.add(btnEdit);
		
		JButton btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				delete();
			}
		});
		panel_2.add(btnDelete);
		
		JPanel panel_3 = new JPanel();
		panel.add(panel_3, BorderLayout.WEST);
		
		panel_3.add(details_btn);
	}
	
	private void details()
	{
		int row_index = staff_table.getSelectedRow();
		if (row_index != -1)
		{
			if( staff_table.getModel().getValueAt(row_index, 0) != null)
			{
				AccountDetails p = new AccountDetails((String.valueOf((Integer) staff_table.getModel().getValueAt(row_index, 0))));
				getDesktopPane().add(p);
				Point current_win = getLocationOnScreen();
				int x = (int) current_win.getX() + 20;
				int y = (int) current_win.getY() + 5;
				p.setLocation(x, y);
				p.setVisible(true);
			}
		}
	}
	
	private void add()
	{
		AccountManager p = new AccountManager(null);
		getDesktopPane().add(p);
		Point current_win = getLocationOnScreen();
		int x = (int) current_win.getX() + 20;
		int y = (int) current_win.getY() + 5;
		p.setLocation(x, y);
		p.setVisible(true);
	}
	
	private void edit()
	{
		int row_index = staff_table.getSelectedRow();
		if (row_index != -1)
		{
			if( staff_table.getModel().getValueAt(row_index, 0) != null)
			{
				AccountManager p = new AccountManager(String.valueOf((Integer) staff_table.getModel().getValueAt(row_index, 0)));
				getDesktopPane().add(p);
				Point current_win = getLocationOnScreen();
				int x = (int) current_win.getX() + 20;
				int y = (int) current_win.getY() + 5;
				p.setLocation(x, y);
				p.setVisible(true);
			}
		}
	}
	
	private void delete()
	{
		int row_index = staff_table.getSelectedRow();
		if (row_index != -1)
		{
			String account_id = String.valueOf(staff_table.getModel().getValueAt(row_index, 0));
			if( account_id != null)
			{
				if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(this,
						"Are you sure you want to delete this account?",
						"Delete account", JOptionPane.YES_NO_OPTION))
				{
					staff_ctr.delete_account(account_id);
					refresh();
				}
			}
		}
	}
	
	private boolean is_numeric(String checker)
	{
		try
		{
			Integer.parseInt(checker);
		}
		catch (NumberFormatException nfe)
		{
			return false;
		}
		return true;
	}

}
